package imu.g3.dao;


import imu.g3.model.MyProjectUploadFiles;
import imu.g3.model.User;
import imu.g3.util.DataUtil;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;


public class UserDao {

    public int register(User user) {
        //获取与MySQL链接的Connection对象
        Connection conn = null;
        PreparedStatement ptst = null;
        int res = 0;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
        try {
            String insert = "insert into user(name,phone,password,regdate,email) values(?,?,?,?,?)";
            conn = new DataUtil().getConnection();
            ptst = conn.prepareStatement(insert);
            ptst.setString(1, user.getName());
            ptst.setString(2,user.getPhone());
            ptst.setString(3, user.getPassword());
            ptst.setString(4, date);
            ptst.setString(5, user.getEmail());
            //执行sql语句
            ptst.execute();
            res = ptst.getUpdateCount();
            //关闭关闭ResultSet和Statement链接
            ptst.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    public List<User> getUserInfo() {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        List<User> userList = new ArrayList<>();
        String getUserInfo = "select * from user";
        try {
            conn = new DataUtil().getConnection();
            stmt = conn.prepareStatement(getUserInfo);
            rs = stmt.executeQuery();
            while (rs.next()) {
                User user = new User();
                user.setName(rs.getString(1));
                user.setPhone(rs.getString(2));
                user.setPassword(rs.getString(3));
                user.setEmail(rs.getString(5));
                user.setIconname(rs.getString(6));
                userList.add(user);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userList;
    }

    public void updateIconName(String username, String fileName) {
        //获取与MySQL链接的Connection对象
        Connection conn = null;
        int res = 0;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
        try {
            String sql="update user set iconname=? where name=?";
            //预置对象
            conn = new DataUtil().getConnection();
            PreparedStatement pstmt=conn.prepareStatement(sql);
            //设置占位符值
            pstmt.setString(1, fileName);
            pstmt.setString(2, username);

            //执行sql语句
            pstmt.executeUpdate();
            res = pstmt.getUpdateCount();
            //关闭关闭ResultSet和Statement链接
            pstmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
